/* eslint-disable no-sparse-arrays */
import language from "./Language";
function calcTaxAmount() {}
function downloadFile({ url, fileName }) {
  if (!fileName) {
    let lastIndex = url.lastIndexOf("/");
    if (lastIndex !== -1) {
      fileName = url.substr(lastIndex + 1);
    }
  }
  const link = document.createElement("a");
  link.download = fileName;
  link.target = "_blank";
  // 创建URL对象.
  // link.href = URL.createObjectURL(url);
  link.href = url;
  document.body.appendChild(link);
  link.click();
  // 释放一个通过URL.createObjectURL()创建的对象URL.
  URL.revokeObjectURL(link.href);
  document.body.removeChild(link);
}
const download = (url) => {
  if (!(/http/i).test(url)) {
    url = process.env.VUE_APP_BASEURL + url;
  }
  downloadFile({ url });
};
function combNav(arr) {
  const obj = {};
  const action_typeList = {};
  for (let i = 0; i < arr.length; i++) {
    const element = arr[i];
    action_typeList[arr[i].name] = arr[i].action_type.split(",");
    element.auths = element.action_type.split(",");
    if (!obj[element.parent_id]) {
      obj[element.parent_id] = { subMenus: [element] };
    } else {
      obj[element.parent_id].subMenus.push(element);
    }
  }

  // this.roleNav = obj2;
  let nav = obj[0].subMenus;
  nav[0].children = obj[nav[0].id].subMenus;
  let array = nav[0].children;
  for (let i = 0; i < array.length; i++) {
    if (obj[array[i].id]) {
      array[i].children = obj[array[i].id].subMenus;
    }
  }
  return { nav, action_typeList };
}
function formatDate(str) {
  if (!str) {
    return "";
  }
  return new Date(str).format("yyyy-MM-dd");
}
const projectPrivacy = (phone) => {
  if (!phone) return;
  let header = phone.slice(0, 3);
  let footer = phone.slice(-4);
  let projectPhone = header + "****" + footer;
  return projectPhone;
};
const dictionary = {
  payType: {
    1: "银行卡",
  },
  invoiceType: {
    1: "全额",
  },
  CheckResult: {
    1: language.local("已审核"),
  },
  nsrxz: {
    1: "一般纳税人",
    2: "小规模纳税人",
  },
  Sex: {
    1: "男",
    2: "女",
  },
  thirdPartyType: {
    PARTNER: "平台商户",
    MERCHANT: "集团商户"

  },
  // pricipalType: {
  //   CARD: "支付宝账号",
  //   CUSTOMER: "支付宝用户"
  // },
  zt: {
    0: "未审核",
    1: "已审核",
    "-1": "未审核",
  },
  signInf: {
    0: "未签约",
    1: "已签约",
  },
  EmpRM: {
    "-1": "否",
    0: "否",
    1: "是",
  },
  // AuditStatus: {
  //   0: "不通过",
  // },
  OpenInvoiceStatus: {
    0: "未开票",
    1: "已开票"
  },
  roletype: {
    2: "服务商用户",
    3: "企业用户",
    4: "税务监管",
    5: "代理商负责人",
    6: "平台财务",
    7: "平台运营主管",
    51: "代理商商务经理",
    71: "平台运营岗",
    99: "平台管理",
  },
  verifysignstatus: ["未验证", "已验证"],
  TskStatus: {
    "-1": "草稿",
    0: "草稿",
    1: "派发中",
    2: "已接单",
    3: "已充值",
    4: "结算中",
    5: "开票中",
    6: "已结束",
    7: "已终止",
  },
  ruleType: {
    A: "A：实发金额 / （1-充值费率）",
    B: "B：实发金额 * （1+充值费率）"
  },
  TskStatusColor: {
    "-1": "#FAAD14",
    0: "#FAAD14",
    1: "#FAAD14",
    2: "#FAAD14",
    3: "#FAAD14",
    4: "#FAAD14",
    5: "#2c933a",
    6: "#2c933a",
  },
  IsDefault: {
    0: "否",
    1: "是",
  },

  Status: ["未审核", "已审核"],
  status: {
    TEMP: "暂存",
    NORMAL: "正常",
    STOP: "暂停"
  },
  paystatus: { "-1": "待支付", 0: "未支付", 1: "已支付", 2: "不通过" },

  BusinesStatus: [, , "银行处理成功"],
  BatchStatus: [, , , , , , "代付处理完成"],
  get: function (key, value) {
    if ((/registeredcapital/).test(key)) {
      return value + "万元";
    }
    if ((/phone|BeneficiaryAccountNumber|BankNo/i).test(key)) {
      return projectPrivacy(value);
    }
    if (!Object.hasOwnProperty.call(this, key)) {
      return value;
    }
    return this[key][value] || value;
  },
};
const toFixed2 = (num) => {
  if (!num) {
    return "0.00";
  }

  if (typeof num == "string") {
    num = Number(num);
  }
  return num.toFixed(2);
};
const common = {
  calcTaxAmount,
  downloadFile,
  download,
  combNav,
  formatDate,
  projectPrivacy,
  dictionary,
  toFixed2,
};
export default common;
